/*
 * DateChooserFrame.java
 *
 * Created on 28 October 2003, 12:48
 */

package com.velocityme.client.gui.calendar;

import java.util.*;

public class CalendarFrame extends javax.swing.JFrame 
    implements CalendarTimerListener {
    
    /** Creates new form DateChooserFrame */
    public CalendarFrame() {
        this("");
    }
    
    public CalendarFrame(String dt) {
        initComponents();
        initCalendar();
        
        if (!dt.equals("")) {
            int year = Integer.parseInt(dt.substring(0,4));
            int month = Integer.parseInt(dt.substring(5,7))-1;
            int day = Integer.parseInt(dt.substring(8,10));
            calendar.set(year, month, day);
            refreshCalendar();
        }        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        northPanel = new javax.swing.JPanel();
        btnYearMinus = new javax.swing.JButton();
        btnMonthMinus = new javax.swing.JButton();
        txtDate = new javax.swing.JTextField();
        btnMonthPlus = new javax.swing.JButton();
        btnYearPlus = new javax.swing.JButton();
        centerPanel = new javax.swing.JPanel();
        scrollPane = new javax.swing.JScrollPane();
        tblDays = new javax.swing.JTable();
        southPanel = new javax.swing.JPanel();
        btnToday = new javax.swing.JButton();
        btnSelect = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Select a date");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        btnYearMinus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/gui/calendar/images/arrow_big_left.gif")));
        btnYearMinus.setToolTipText("Back a year");
        btnYearMinus.setBorder(null);
        btnYearMinus.setFocusable(false);
        btnYearMinus.setMaximumSize(new java.awt.Dimension(20, 20));
        btnYearMinus.setMinimumSize(new java.awt.Dimension(20, 20));
        btnYearMinus.setPreferredSize(new java.awt.Dimension(20, 20));
        btnYearMinus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnYearMinusActionPerformed(evt);
            }
        });
        btnYearMinus.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                btnYearMinusMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                btnYearMinusMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                btnYearMinusMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnYearMinusMouseReleased(evt);
            }
        });

        northPanel.add(btnYearMinus);

        btnMonthMinus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/gui/calendar/images/arrow_small_left.gif")));
        btnMonthMinus.setToolTipText("Back a month");
        btnMonthMinus.setBorder(null);
        btnMonthMinus.setFocusable(false);
        btnMonthMinus.setMaximumSize(new java.awt.Dimension(20, 20));
        btnMonthMinus.setMinimumSize(new java.awt.Dimension(20, 20));
        btnMonthMinus.setPreferredSize(new java.awt.Dimension(20, 20));
        btnMonthMinus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMonthMinusActionPerformed(evt);
            }
        });
        btnMonthMinus.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                btnMonthMinusMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                btnMonthMinusMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                btnMonthMinusMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnMonthMinusMouseReleased(evt);
            }
        });

        northPanel.add(btnMonthMinus);

        txtDate.setBackground(new java.awt.Color(255, 255, 204));
        txtDate.setEditable(false);
        txtDate.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtDate.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
        txtDate.setMaximumSize(new java.awt.Dimension(130, 19));
        txtDate.setMinimumSize(new java.awt.Dimension(130, 19));
        txtDate.setPreferredSize(new java.awt.Dimension(130, 19));
        northPanel.add(txtDate);

        btnMonthPlus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/gui/calendar/images/arrow_small_right.gif")));
        btnMonthPlus.setToolTipText("Advance a month");
        btnMonthPlus.setBorder(null);
        btnMonthPlus.setFocusable(false);
        btnMonthPlus.setMaximumSize(new java.awt.Dimension(20, 20));
        btnMonthPlus.setMinimumSize(new java.awt.Dimension(20, 20));
        btnMonthPlus.setPreferredSize(new java.awt.Dimension(20, 20));
        btnMonthPlus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMonthPlusActionPerformed(evt);
            }
        });
        btnMonthPlus.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                btnMonthPlusMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                btnMonthPlusMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                btnMonthPlusMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnMonthPlusMouseReleased(evt);
            }
        });

        northPanel.add(btnMonthPlus);

        btnYearPlus.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/velocityme/client/gui/calendar/images/arrow_big_right.gif")));
        btnYearPlus.setToolTipText("Advance a year");
        btnYearPlus.setBorder(null);
        btnYearPlus.setFocusable(false);
        btnYearPlus.setMaximumSize(new java.awt.Dimension(20, 20));
        btnYearPlus.setMinimumSize(new java.awt.Dimension(20, 20));
        btnYearPlus.setPreferredSize(new java.awt.Dimension(20, 20));
        btnYearPlus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnYearPlusActionPerformed(evt);
            }
        });
        btnYearPlus.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                btnYearPlusMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                btnYearPlusMouseExited(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                btnYearPlusMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnYearPlusMouseReleased(evt);
            }
        });

        northPanel.add(btnYearPlus);

        getContentPane().add(northPanel, java.awt.BorderLayout.NORTH);

        centerPanel.setLayout(new java.awt.BorderLayout());

        scrollPane.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        scrollPane.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                scrollPaneComponentResized(evt);
            }
        });

        tblDays.setModel(tblDays.getModel());
        tblDays.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        tblDays.setCellSelectionEnabled(true);
        tblDays.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblDaysMouseClicked(evt);
            }
        });
        tblDays.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                tblDaysMouseDragged(evt);
            }
        });

        scrollPane.setViewportView(tblDays);

        centerPanel.add(scrollPane, java.awt.BorderLayout.CENTER);

        getContentPane().add(centerPanel, java.awt.BorderLayout.CENTER);

        btnToday.setText("Today");
        btnToday.setToolTipText("Go to today");
        btnToday.setMaximumSize(new java.awt.Dimension(75, 25));
        btnToday.setMinimumSize(new java.awt.Dimension(75, 25));
        btnToday.setPreferredSize(new java.awt.Dimension(75, 25));
        btnToday.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTodayActionPerformed(evt);
            }
        });

        southPanel.add(btnToday);

        btnSelect.setText("Select");
        btnSelect.setToolTipText("Select the highlighted date");
        btnSelect.setMaximumSize(new java.awt.Dimension(75, 25));
        btnSelect.setMinimumSize(new java.awt.Dimension(75, 25));
        btnSelect.setPreferredSize(new java.awt.Dimension(75, 25));
        btnSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectActionPerformed(evt);
            }
        });

        southPanel.add(btnSelect);

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        southPanel.add(btnCancel);

        getContentPane().add(southPanel, java.awt.BorderLayout.SOUTH);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-270)/2, (screenSize.height-230)/2, 270, 230);
    }//GEN-END:initComponents

    private void btnMonthMinusMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnMonthMinusMouseReleased
        stopTimer();
    }//GEN-LAST:event_btnMonthMinusMouseReleased

    private void btnMonthMinusMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnMonthMinusMousePressed
        currentButton = MONTH_MINUS;
        startTimer();
    }//GEN-LAST:event_btnMonthMinusMousePressed

    private void btnMonthPlusMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnMonthPlusMouseReleased
        stopTimer();
    }//GEN-LAST:event_btnMonthPlusMouseReleased

    private void btnMonthPlusMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnMonthPlusMousePressed
        currentButton = MONTH_PLUS;
        startTimer();
    }//GEN-LAST:event_btnMonthPlusMousePressed

    private void btnYearMinusMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnYearMinusMouseReleased
        stopTimer();
    }//GEN-LAST:event_btnYearMinusMouseReleased

    private void btnYearMinusMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnYearMinusMousePressed
        currentButton = YEAR_MINUS;
        startTimer();
    }//GEN-LAST:event_btnYearMinusMousePressed

    private void btnYearPlusMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnYearPlusMousePressed
        currentButton = YEAR_PLUS;
        startTimer();
    }//GEN-LAST:event_btnYearPlusMousePressed

    private void btnYearPlusMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnYearPlusMouseReleased
        stopTimer();
    }//GEN-LAST:event_btnYearPlusMouseReleased

    private void tblDaysMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblDaysMouseDragged
        int row = tblDays.getSelectedRow();
        int col = tblDays.getSelectedColumn();
        String s = (String)tblDays.getValueAt(row, col);
        if (!s.equals("")) {
            calendar.set(Calendar.DATE, Integer.parseInt(s));
            refreshCalendar();
        }
    }//GEN-LAST:event_tblDaysMouseDragged

    private void tblDaysMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblDaysMouseClicked
        int clickCount = evt.getClickCount();
        int row = tblDays.getSelectedRow();
        int col = tblDays.getSelectedColumn();
        String s = (String)tblDays.getValueAt(row, col);
        if (!s.equals("")) {
            if (clickCount == 1) {
                calendar.set(Calendar.DATE, Integer.parseInt(s));
                refreshCalendar();                
            }
            else if (clickCount == 2) {
                calendar.set(Calendar.DATE, Integer.parseInt(s));
                setSelectAction();
            }
        }
    }//GEN-LAST:event_tblDaysMouseClicked

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private void scrollPaneComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_scrollPaneComponentResized
        resizeTable();
    }//GEN-LAST:event_scrollPaneComponentResized

    private void btnSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectActionPerformed
        int row = tblDays.getSelectedRow();
        int col = tblDays.getSelectedColumn();
        String s = (String)tblDays.getValueAt(row, col);
        if (!s.equals("")) {        
            calendar.set(Calendar.DATE, Integer.parseInt(s));
            setSelectAction();
        }
    }//GEN-LAST:event_btnSelectActionPerformed

    private void setSelectAction() {
        if (textFieldListener != null) {
            textFieldListener.setText(getCalendarDateString(calendar));
        }
        
        if (calendarListener != null) {
            calendarListener.setSelectAction(getCalendarDateString(calendar));
        }
        
        this.dispose();        
    }
    
    private void btnYearPlusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnYearPlusActionPerformed
        calendar.add(Calendar.YEAR, 1);
        refreshCalendar();
    }//GEN-LAST:event_btnYearPlusActionPerformed

    private void btnYearMinusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnYearMinusActionPerformed
        calendar.add(Calendar.YEAR, -1);
        refreshCalendar();
    }//GEN-LAST:event_btnYearMinusActionPerformed

    private void btnMonthPlusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMonthPlusActionPerformed
        calendar.add(Calendar.MONTH, 1);
        refreshCalendar();
    }//GEN-LAST:event_btnMonthPlusActionPerformed

    private void btnMonthMinusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnMonthMinusActionPerformed
        calendar.add(Calendar.MONTH, -1);
        refreshCalendar();
    }//GEN-LAST:event_btnMonthMinusActionPerformed

    private void btnYearMinusMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnYearMinusMouseExited
        btnYearMinus.setBorder(null);
    }//GEN-LAST:event_btnYearMinusMouseExited

    private void btnYearMinusMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnYearMinusMouseEntered
        btnYearMinus.setBorder(new javax.swing.border.EtchedBorder());
    }//GEN-LAST:event_btnYearMinusMouseEntered

    private void btnMonthMinusMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnMonthMinusMouseExited
        btnMonthMinus.setBorder(null);
    }//GEN-LAST:event_btnMonthMinusMouseExited

    private void btnMonthMinusMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnMonthMinusMouseEntered
        btnMonthMinus.setBorder(new javax.swing.border.EtchedBorder());
    }//GEN-LAST:event_btnMonthMinusMouseEntered

    private void btnYearPlusMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnYearPlusMouseEntered
        btnYearPlus.setBorder(new javax.swing.border.EtchedBorder());        
    }//GEN-LAST:event_btnYearPlusMouseEntered

    private void btnYearPlusMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnYearPlusMouseExited
        btnYearPlus.setBorder(null);
    }//GEN-LAST:event_btnYearPlusMouseExited

    private void btnMonthPlusMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnMonthPlusMouseExited
        btnMonthPlus.setBorder(null);
    }//GEN-LAST:event_btnMonthPlusMouseExited

    private void btnMonthPlusMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnMonthPlusMouseEntered
        btnMonthPlus.setBorder(new javax.swing.border.EtchedBorder());
    }//GEN-LAST:event_btnMonthPlusMouseEntered

    private void btnTodayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTodayActionPerformed
        calendar = Calendar.getInstance();
        refreshCalendar();
    }//GEN-LAST:event_btnTodayActionPerformed
   
    private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
        this.dispose();
    }//GEN-LAST:event_exitForm

    
    public void setDate(String dt) {
        Calendar cal = Calendar.getInstance();
		try {
            cal.set(Calendar.YEAR, Integer.parseInt(dt.substring(0,4)));
            cal.set(Calendar.MONTH, Integer.parseInt(dt.substring(5,7)));
            cal.set(Calendar.DATE, Integer.parseInt(dt.substring(8,10)));
            setCalendarToDate(cal);
        }
		catch (NumberFormatException nfe) {
            return;
        }
        
        
    }
    
    private void initCalendar() {
        calendar = Calendar.getInstance();
        calendarTableModel = new CalendarTableModel();
		calendarTableModel.addColumn("Sun");
		calendarTableModel.addColumn("Mon");
		calendarTableModel.addColumn("Tue");
		calendarTableModel.addColumn("Wed");
		calendarTableModel.addColumn("Thu");
		calendarTableModel.addColumn("Fri");
		calendarTableModel.addColumn("Sat");

		tblDays.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
		tblDays.getTableHeader().setReorderingAllowed(false);
		tblDays.getTableHeader().setResizingAllowed(false);
        
		//color code the table
		try {
			tblDays.setDefaultRenderer(Class.forName("java.lang.String"), new DayCellRenderer());
		}
		catch (Exception e) {
			System.out.println("error:" + e);
		}        
        
        tblDays.setModel(calendarTableModel);        
        refreshCalendar();
    }
    
    private void refreshCalendar() {  
        if (START_YEAR < calendar.get(Calendar.YEAR) && 
        END_YEAR > calendar.get(Calendar.YEAR)) {
            setDays();
            setCalendarToDate(calendar);
            txtDate.setText(getDayDisplay(calendar) + 
                " " + getMonthDisplay(calendar) +
                " " + getYearDisplay(calendar));
            resizeTable();
        }
        else {
            stopTimer();
        }
    }
        
    
	private void setCalendarToDate(Calendar cal) {
        boolean breakFromOuterLoop = false;
        int dayOfWeek = cal.get(Calendar.DAY_OF_MONTH);

        for (int row = 0; row < calendarTableModel.getRowCount(); row++) {
           for (int col = 0; col < 7; col++) {
              try {
                 int currentDay = Integer.parseInt((String)calendarTableModel.getValueAt(row,col));
                 if(currentDay == dayOfWeek){
                    tblDays.changeSelection(row,col,false,false);
                    breakFromOuterLoop = true;
                    break;
                 }
              }
              catch(NumberFormatException nfe) {
                  /*ignore*/
              }
           }
           
           if (breakFromOuterLoop)
               break;
        }
	}    

	private void setDays() {
		calendarTableModel.clearData();
		Calendar tempCal = (Calendar)calendar.clone();
		tempCal.set(Calendar.DAY_OF_MONTH,1);
		int startCol = tempCal.get(Calendar.DAY_OF_WEEK)-1;
		int maxDays = getDaysInMonth(tempCal);
		int lastUsed = 0;

		for (int row = 0; row < 6; row++) {
			if (lastUsed >= maxDays) break;
			lastUsed = addDaysRow(row, startCol, lastUsed, maxDays);
			startCol = 0;
		}
	}
    
	private int addDaysRow(int row, int startCol, int lastUsed, int maxDays) {
		calendarTableModel.addRow(new Object[] {"","","","","","",""});
        String[] s = new String[7];
		for (int colNum=startCol; colNum < 7; colNum++) {
			if (++lastUsed > maxDays) break;
			calendarTableModel.setValueAt("" + lastUsed, row, colNum);
		}
		return lastUsed;
	}    
    
    public void resizeTable() {
		int tblSpace = scrollPane.getHeight() - tblDays.getTableHeader().getHeight();
		int rowHeight = (tblSpace-1) / calendarTableModel.getRowCount();
		if (calendarTableModel.getRowCount() > 0 && tblSpace > 0) {
			tblDays.setRowHeight(rowHeight);
		}
	}   
    
    private String getYearDisplay(Calendar cal) {
        return cal.get(Calendar.YEAR) + "";
    }
    
    private String getMonthDisplay(Calendar cal) {
        String s = "";
        switch (cal.get(Calendar.MONTH)) {
            case Calendar.JANUARY:
                s = "January";
                break;
            case Calendar.FEBRUARY:
                s = "February";
                break;
            case Calendar.MARCH:
                s = "March";
                break;
            case Calendar.APRIL:
                s = "April";
                break;
            case Calendar.MAY:
                s = "May";
                break;
            case Calendar.JUNE:
                s = "June";
                break;
            case Calendar.JULY:
                s = "July";
                break;
            case Calendar.AUGUST:
                s = "August";
                break;
            case Calendar.SEPTEMBER:
                s = "September";
                break;
            case Calendar.OCTOBER:
                s = "October";
                break;
            case Calendar.NOVEMBER:
                s = "November";
                break;
            case Calendar.DECEMBER:
                s = "December";
                break;
        }
        
        return s;
    }
    
    private String getDayDisplay(Calendar cal) {
        return (cal.get(Calendar.DATE) + "");
    }
    
	private int getDaysInMonth(Calendar c) {
		int numDays = 0;

		switch (c.get(Calendar.MONTH)) {
			case Calendar.JANUARY:
			case Calendar.MARCH:
			case Calendar.MAY:
			case Calendar.JULY:
			case Calendar.AUGUST:
			case Calendar.OCTOBER:
			case Calendar.DECEMBER:
				numDays = 31;
				break;
			case Calendar.APRIL:
			case Calendar.JUNE:
			case Calendar.SEPTEMBER:
			case Calendar.NOVEMBER:
				numDays = 30;
				break;
			case Calendar.FEBRUARY:
				numDays = getFebDays(c.get(Calendar.YEAR));
				break;
		}
		
		return numDays;
	}

	private int getFebDays(int y) {
		int febDays = 0;
		if ((y % 4) != 0)
			febDays = 28;
		else if ((y % 400) == 0)
			febDays = 29;
		else if ((y % 100) == 0)
			febDays = 28;
		else
			febDays = 29;

		return febDays;

	} 
    
	private String getCalendarDateString(Calendar calendar) {
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get((Calendar.MONTH)) + 1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
			
		StringBuffer dt = new StringBuffer(10);
        
        dt.append(year);
        dt.append("/");

		if (month < 10) {
			dt.append("0");
        }
		dt.append(month);
        dt.append("/");

		if (day < 10) {
			dt.append("0");
        }
		dt.append(day);

		return dt.toString();
	}  
    
    public void registerListener(javax.swing.JTextField listener) {
        textFieldListener = listener;
    }
    
    public void registerListener(CalendarListener listener) {
        calendarListener = listener;
    }
    
    public void timeElapsed(CalendarTimer t) {        
        switch (currentButton) {
            case YEAR_PLUS:
                calendar.add(Calendar.YEAR, 1);                    
                break;
            case YEAR_MINUS:
                calendar.add(Calendar.YEAR, -1);       
                break;
            case MONTH_PLUS:
                calendar.add(Calendar.MONTH, 1);      
                break;
            case MONTH_MINUS:
                calendar.add(Calendar.MONTH, -1);      
                break;
        }
        refreshCalendar();
    }
    
    private void startTimer() {
        defaultTimer = new CalendarTimer(this, currentButton);
        defaultTimer.start();        
    }
    
    private void stopTimer() {
        if (defaultTimer != null) {
            currentButton = -1;
            defaultTimer.interrupt();
            //defaultTimer = null;
        }
        //refreshCalendar();
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnMonthMinus;
    private javax.swing.JButton btnMonthPlus;
    private javax.swing.JButton btnSelect;
    private javax.swing.JButton btnToday;
    private javax.swing.JButton btnYearMinus;
    private javax.swing.JButton btnYearPlus;
    private javax.swing.JPanel centerPanel;
    private javax.swing.JPanel northPanel;
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JPanel southPanel;
    private javax.swing.JTable tblDays;
    private javax.swing.JTextField txtDate;
    // End of variables declaration//GEN-END:variables
    
    private CalendarTableModel calendarTableModel;
    private Calendar calendar;
    private javax.swing.JTextField textFieldListener;
    private CalendarListener calendarListener;
    private CalendarTimer defaultTimer;
    public static final int YEAR_PLUS = 0;
    public static final int YEAR_MINUS = 1;
    public static final int MONTH_PLUS = 2;
    public static final int MONTH_MINUS = 3;
    private int currentButton = -1;
    private ArrayList calendarData;
    private final int START_YEAR = 1900;
    private final int END_YEAR = 2100;
}

class DayCellRenderer extends javax.swing.table.DefaultTableCellRenderer {
    
	public java.awt.Component getTableCellRendererComponent(javax.swing.JTable parent,
        Object value, boolean isSelected, boolean hasFocus, int row, int col) {

		java.awt.Component cell = super.getTableCellRendererComponent(parent, 
            value,isSelected, hasFocus, row, col);

		if (value == null || value.toString().equalsIgnoreCase("")) {
			cell.setBackground(java.awt.Color.GRAY);
		}
		else if (parent.getSelectedColumn() == col && parent.getSelectedRow() == row) {
			cell.setBackground(java.awt.Color.BLUE);
			cell.setForeground(java.awt.Color.WHITE);
		}
		else {
			cell.setBackground(java.awt.Color.WHITE);
			cell.setForeground(java.awt.Color.BLACK);
		}
		return cell;
	}
}
